/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processor;

import entity.Category;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author maynoigi
 */
@Stateless
public class CategoryFacade extends AbstractFacade<Category> {
    @PersistenceContext(unitName = "ManTechHelpdesk-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CategoryFacade() {
        super(Category.class);
    }
     public List<Category> getCategoryAll() {
        return em.createQuery("SELECT c FROM Category c Order by c.categoryID DESC").getResultList();
    }

    public Category getCategory(Integer categoryID) {
        try {
            Category cate = (Category) em.createNamedQuery("Category.findByCategoryID")
                    .setParameter("categoryID", categoryID).getSingleResult();
            return cate;
        } catch (NoResultException e) {
            System.out.println(e.getMessage());
            return null;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    public Category getCategory(String categoryName) {
        try {
            Category cate = (Category) em.createNamedQuery("Category.findByCategoryName")
                    .setParameter("categoryName", categoryName).getSingleResult();
            return cate;
        } catch (NoResultException e) {
            System.out.println(e.getMessage());
            return null;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    public boolean createCategory(String categoryName, String categoryPriority) {
        try {
            Category cate = new Category();
            cate.setCategoryName(categoryName);
            cate.setCategoryPriority(categoryPriority);
            em.persist(cate);
            return true;
        } catch (EntityExistsException e) {
            System.out.println(e.getMessage());
            return false;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public boolean editCategory(Integer categoryID, String categoryName, String categoryPriority) {
        try {
            Category cate = new Category(categoryID);
            cate.setCategoryName(categoryName);
            cate.setCategoryPriority(categoryPriority);
            em.merge(cate);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
    
    public boolean removeCategory(Integer departmentID){
        try {
            Category cate = getCategory(departmentID);
            em.remove(cate);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}
